let app = getApp();

Page({

    /**
     * 页面的初始数据 15013895866
     */
    data: {
      account: '',
      code: '',
      radio:1
    },

    /** 账号输入 */
    inputAccount(e){
      this.setData({ account: e.detail.value });
    },

    inputCode(e){
      this.setData({ code: e.detail.value });
    },

    sendCode(){
      // let reg = /^1\d{10}$/;
      if(this.data.account == ''){
        app.showFailToast('请输入账号');
        return false;
      }
      // if(!reg.test(this.data.account)){
      //   app.showFailToast('请输入正确的手机号');
      //   return false;
      // }

    },

    submit(){

      var radio = this.data.radio;

      if(radio==2){
        wx.setStorageSync('account', this.data.account);
        wx.setStorageSync('password', this.data.code);
        wx.setStorageSync('radio', this.data.radio);
      }else{
        wx.setStorageSync('account', "");
        wx.setStorageSync('password',"");
        wx.setStorageSync('radio',1);
      }

      // let reg = /^1\d{10}$/;
      if(this.data.account == ''){
        app.showFailToast('请输入账号');
        return false;
      }
      // if(!reg.test(this.data.account)){
      //   app.showFailToast('请输入正确的手机号');
      //   return false;
      // }
      if(this.data.code == ''){
        app.showFailToast('请输入密码');
        return false;
      }
      let params = {
        appid: app.globalData.appid,
        mobile: this.data.account,
        password: this.data.code
      }
      wx.request({
        url: `${app.globalData.rootUrl}/api/Login/dologin`,
        method: 'post',
        data: params,
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          console.log(res);
          if (0 == res.data.code) {
            wx.setStorageSync('user', res.data.user);
            wx.setStorageSync('cookie', res.cookies[0]);
            wx.switchTab({ url: '/pages/index/index' });
          } else {
            app.showFailToast(res.data.msg);
          }
    
        }
      })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      var account = wx.getStorageSync('account');
      var password = wx.getStorageSync('password');
      var radio = wx.getStorageSync('radio');
      if(radio!=2){
        radio = 1;
      }
      
      this.setData({account:account,code:password,radio:radio})
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    onRadio:function(){
      let radio = this.data.radio;
      if(radio==1){
        radio = 2;
      }else{
        radio = 1;
      }
      this.setData({radio:radio});

    }
})